package ovm.management.exam;

import ovm.entity.*;
import ovm.management.AbstractManager;

import javax.persistence.Query;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Jokser
 * Date: 15.06.12
 * Time: 0:15
 * To change this template use File | Settings | File Templates.
 */
public class ExamManager extends AbstractManager {

    public static int getFinalRating (int preRating, int exaRating) {
        return Math.round(preRating * 0.6f + exaRating * 0.4f);
    }
    
    public static int getSingleRating (int finalRating) {
        if (finalRating >= 95) return 9;
        if (finalRating >= 90) return 8;
        if (finalRating >= 85) return 7;
        if (finalRating >= 80) return 6;
        if (finalRating >= 75) return 5;
        if (finalRating >= 70) return 4;
        if (finalRating >= 65) return 3;
        if (finalRating >= 60) return 2;
        if (finalRating >= 55) return 1;
        if (finalRating >= 50) return 0;
        return -1;
    }
    
    public static String getCharRating (int singleRating) {
        if (singleRating == 9) return "A";
        if (singleRating == 8) return "A-";
        if (singleRating == 7) return "B+";
        if (singleRating == 6) return "B";
        if (singleRating == 5) return "B-";
        if (singleRating == 4) return "C+";
        if (singleRating == 3) return "C";
        if (singleRating == 2) return "C-";
        if (singleRating == 1) return "D+";
        if (singleRating == 0) return "D";
        return "E";
    }
    
    public static int persistExamBlank (ExamBlankEntity entity) {
        int finalRating = getFinalRating(entity.getPreRating(), entity.getExaRating());
        int singleRating = getSingleRating(finalRating);
        String charRating = getCharRating(singleRating);
        
        try {
            entity.setFinalRating(finalRating);
            entity.setSingleRating(singleRating);
            entity.setCharRating(charRating);
            
            em.getTransaction().begin();
            em.persist(entity);
            em.getTransaction().commit();
            
        } catch (Exception e) {
            e.printStackTrace();
            em = emf.createEntityManager();
            return UNSUCCESSFUL;
        }
        return SUCCESSFUL;
    }
    
    public static int deleteBlank() {
        try {
            em.getTransaction().begin();

            Query query = em.createQuery("SELECT e FROM ExamBlankEntity e");

            List<ExamBlankEntity> list = query.getResultList();

            for (ExamBlankEntity entity : list) {
                em.remove(entity);
            }

            em.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
            em = emf.createEntityManager();
            return UNSUCCESSFUL;
        }
        return SUCCESSFUL;        
    }
    
    public static int acceptBlank (Date examDate, SubjectEntity subject, StudyGroupEntity studyGroup) {
        try {
            em.getTransaction().begin();
            
            Query query = em.createQuery("SELECT e FROM ExamBlankEntity e");

            List<ExamBlankEntity> list = query.getResultList();

            for (ExamBlankEntity entity : list) {
                ExamEntity exam = new ExamEntity();
                exam.setExamDate(examDate);
                exam.setStudent(entity.getStudent());
                exam.setSubject(subject);
                exam.setPreRating(entity.getPreRating());
                exam.setExaRating(entity.getExaRating());
                exam.setFinalRating(entity.getFinalRating());
                exam.setSingleRating(entity.getSingleRating());
                exam.setCharRating(entity.getCharRating());
                em.persist(exam);
            }

            em.getTransaction().commit();

        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
            em = emf.createEntityManager();
            return UNSUCCESSFUL;
        }
        return SUCCESSFUL;
    }

}
